<template>
    <view class="content">
		<image src="https://datiimg.nomeis.cn/start.png" class="float-img"></image>
		<view id="auth" class="btn-row">
			<button
			v-if="canIUse" 
			open-type="getUserInfo"  
			lang="zh_CN"
			class="l-butn"
			@getuserinfo="bindGetUserInfo"
			plain="true"></button>
		</view>
    </view>
</template>

<script>
    // import {
    //     mapState
    // } from 'vuex'
	import comm from '../../common/helpers.js'
	import mutil from "../../static/js/util.js"
    export default {
		components:{},
		data(){
			return{
				image:'https://zhanbu.nomeis.cn/upload/page_resource/kapai_face.png',
				canIUse: uni.canIUse('button.open-type.getUserInfo'),
				code:'',
				oid:''
			}
		},
        onLoad() {
			comm.login()
        },
		onShow() {
			comm.login()
		},
		methods:{
			queryUsreInfo(e){
				let that = this
				comm.globalData1 = uni.getStorageSync('globalData')
				 uni.request({
					url: comm.websiteUrl+'?s=v1/getuserid/'+ comm.globalData1.data.openid, 
					method: 'GET',
					data: {},
					success: res => {
						// console.log(res)
						if(res.data.code == 404 || res.data.data == null){
							//进行保存用户操作
							uni.request({
								url: comm.websiteUrl + '?s=v1/useradd/',
								method: 'POST',
								data: {
									openid: comm.globalData1.data.openid,
									u_name: e.detail.userInfo.nickName,
									u_head: e.detail.userInfo.avatarUrl,
									province:e.detail.userInfo.province,
									city: e.detail.userInfo.city,
									country:e.detail.userInfo.country,
									gender:e.detail.userInfo.gender == 1?"男":"女"
								},
								header: {
									'content-type':'application/x-www-form-urlencoded'
								},
								success: function (res) {
									uni.switchTab({
										url: '/pages/divine/main'  
									})
									//从数据库获取用户信息
									// that.queryUsreInfo(e.detail.userInfo.nickName);
									// console.log("插入小程序登录用户信息成功！");
								}
							});
						}else{
							uni.switchTab({
								url: '/pages/divine/main'  
							})
						}
					},
					fail: () => {},
					complete: () => {
					}
				});
			},
			bindGetUserInfo(e) {
				//用户按了允许授权按钮
				let that = this;
				//判断数据库是否存在用户，不存在则显示授权按钮，并存入数据库中
				// console.log(e)
				if (e.detail.userInfo) {
					//插入登录的用户的相关信息到数据库
					//获取openID
					that.addUser(e)
					// uni.switchTab({
					// 	url: '/pages/divine/main'  
					// })
					//授权成功后，跳转进入小程序首页
				} else {
					//用户按了拒绝按钮
					uni.showModal({
						title:'警告',
						content:'您点击了拒绝授权，将无法使用小程序，请授权之后再使用!!!',
						showCancel:false,
						confirmText:'返回授权',
						success:function(res){
							// uni.switchTab({
							// 	url:'/pages/divine/main'
							// })
						}
					})
				}
			},
			addUser(e){
				// console.log('-----------------打印openid------------------')
				let that = this
				// console.log(comm.globalData.data.openid)
				that.queryUsreInfo(e)
			},
			// login(){
			// 	let that = this
			// 	//如果未授权，进行授权，并且判断用户是否存在数据库，不存在则提示授权
			// 	uni.login({
			// 		provider: 'weixin',
			// 		success: res => {
			// 			that.code = res.code //返回code
			// 			that.getOpenid()
			// 		},
			// 		fail: () => {},
			// 		complete: () => {
			// 			
			// 		}
			// 	});
			// },
			// getOpenid(){
			// 	let that = this
			// 	uni.request({
			// 		url: comm.websiteUrl + '?s=v1/getuseropenid/',
			// 		method: 'POST',
			// 		data: {
			// 			'appid' : comm.appId,
			// 			'appsecret':comm.appSecret,
			// 			'code':that.code
			// 		},
			// 		success: res => {
			// 			uni.setStorageSync('openid',res.data.openid)
			// 			that.oid = res
			// 			comm.globalData = res
			// 		},
			// 		fail: () => {},
			// 		complete: () => {}
			// 	});
			// }
		},
		computed:{
		}
    }
</script>

<style lang="scss">
	.content{
		padding: 0upx;
		background-image: url("https://zhanbu.nomeis.cn/upload/page_resource/bg_not_kapai.jpg");
		background-size: 100% 100%;
		
		.float-img{
			width: 60%;
			height: 60%;
			position: absolute;
			margin-left:20%;
			margin-top:20%;
		}
		
		.btn-row{
			width: 100%;
			height: 200upx;
			position: absolute;
			bottom: 0;
			padding: 0upx;
			display: flex;
			justify-content: center;
			
			.l-butn{
				width: 100upx;
				height: 100upx;
				padding: 0upx;
				border: none;
				background-image: url('https://datiimg.nomeis.cn/next_auth.png');
				background-size: 100% 100%;
			}
		}
		
		
	}
</style>
